package com.ylx.web;


import com.alibaba.fastjson.JSON;
import com.ylx.pojo.Goods;
import com.ylx.pojo.PageBean;
import com.ylx.service.GoodsService;
import com.ylx.service.GoodsServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet("/goods/*")
public class GoodsServlet extends BaseServlet {
    private GoodsService goodsService = new GoodsServiceImpl();

    public void selectAll (HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<Goods> goods = goodsService.selectAll();

        String jsonString = JSON.toJSONString(goods);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }

    public void add (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        BufferedReader br = request.getReader();
        String params = br.readLine();

        Goods goods = JSON.parseObject(params, Goods.class);


        goodsService.add(goods);


        response.getWriter().write("success");
    }

    public void delete (HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1. 接收品牌数据
        BufferedReader br = request.getReader();
        String params = br.readLine();
        //转为Brand对象
        int[] ids = JSON.parseObject(params, int[].class);
        goodsService.delete(ids);
        response.getWriter().write("success");
    }

    public void selectByPage (HttpServletRequest request, HttpServletResponse response) throws IOException {
        //1. 接收品牌数据
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        PageBean<Goods> pageBean = goodsService.selectByPage(currentPage, pageSize);

        String jsonString = JSON.toJSONString(pageBean);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }
    public void  selectByPageCondition(HttpServletRequest request, HttpServletResponse response) throws IllegalArgumentException,IOException {
        //1. 接收品牌数据
        String _currentPage = request.getParameter("currentPage");
        String _pageSize = request.getParameter("pageSize");
        int currentPage = Integer.parseInt(_currentPage);
        int pageSize = Integer.parseInt(_pageSize);

        // 获取查询条件对象
        BufferedReader br = request.getReader();
        String params = br.readLine();//json字符串
        //转为 Brand
        Goods goods = JSON.parseObject(params,Goods.class);

        PageBean<Goods> pageBean = goodsService.selectByPageCondition(currentPage,pageSize,goods);

        String jsonString = JSON.toJSONString(pageBean);

        response.setContentType("text/json;charset=utf-8");
        response.getWriter().write(jsonString);
    }


}
